
package ru.ifmo.mlarinsky.classificator.gui;

import java.awt.Dimension;
import java.util.List;
import javax.swing.*;

import net.miginfocom.swing.MigLayout;
import ru.ifmo.mlarinsky.classificator.classification.Classification;
import ru.ifmo.mlarinsky.classificator.classification.DJClassification;
import ru.ifmo.mlarinsky.classificator.model.Instrument;

public class MapLegendPanel extends JPanel {
    private final Classification classification;

    public MapLegendPanel(Classification classification) {
        super(new MigLayout("fill, flowy, ins 0, gap 0"));
        this.classification = classification;

        initLayout();
    }

    private void initLayout() {
        List<Instrument> indexes = classification.getIndexes();

        add(new JLabel("Legend", JLabel.CENTER), "north, left, grow, pushx");
        add(new JSeparator(JSeparator.HORIZONTAL), "north, grow, pushx");
        for (Instrument index : indexes) {
            String indexSymbol = index.getSymbol();
            add(new LegendSectorPanel(
                    classification.getIndexSector(indexSymbol),
                    classification.getIndexColor(indexSymbol)), "north");
        }

        add(new JPanel(), "grow, pushy");
    }

    public static void main(String[] args) {
        testMapLegendPanel();
    }

    private static void testMapLegendPanel() {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                MapLegendPanel panel = new MapLegendPanel(new DJClassification());

                JPanel contentPane = new JPanel(new MigLayout("fill, ins 0"));
                contentPane.add(panel, "center");

                JFrame mainFrame = new JFrame();
                mainFrame.setContentPane(contentPane);
                mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                mainFrame.setSize(new Dimension(200, 500));
                mainFrame.setVisible(true);
            }
        });
    }
}
